import React, { FC } from 'react'
import { Typography, Space, Button, 
    // Checkbox,
    Form, Input, 
    message} from 'antd'
import { UserAddOutlined } from '@ant-design/icons'
import { Link,useNavigate } from 'react-router-dom'
import { registerService } from '../service/user.ts'
import { useRequest } from 'ahooks'
import './Register.css'
const { Title } = Typography
const Register: FC = () => {

    const nav = useNavigate()
    const {run} = useRequest(
        async (values)=>{
            const { username, password, nickname } = values
            await registerService(username, password, nickname)
        },
        {
            manual: true,
            onSuccess:()=>{
                message.success('注册成功')
                nav('/login')
            }
        }
    )
    const onFinish = (values:any) => {
        console.log('Success:', values);
        run(values)
    };
    const onFinishFailed = (errorInfo:any) => {
        console.log('Failed:', errorInfo);
    };
    return (
        <div className="containerRegister">
            <div>
                <Space>
                    <Title level={2}>
                        <UserAddOutlined />
                    </Title>
                    <Title level={2}>
                        注册新用户
                    </Title>
                </Space>
            </div>
            <div>
                <Form
                    name="basic"
                    labelCol={{span: 8}}
                    wrapperCol={{span: 16}}
                    style={{maxWidth: 600}}
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item
                        label="用户名"
                        name="username"
                        rules={[ 
                            {required: true, message: '请输入用户名'},
                            {type: 'string',min:5,max:20,message:'字符长度在5-20之间'},
                            {pattern: /^\w+$/, message:"只能是字母数字下划线"}
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="密码"
                        name="password"
                        rules={[ {required: true,message: '请输入密码'}]}
                    >
                        <Input.Password />
                    </Form.Item>
                    <Form.Item
                        label="确认密码"
                        name="confirm"
                        dependencies={['password']} // 依赖于password
                        rules={[ 
                            {required: true,message: '请输入密码!'},
                            ({ getFieldValue })=>({
                                validator(_,value){
                                    if(!value || getFieldValue('password') === value){
                                        return Promise.resolve()
                                    }else{
                                        return Promise.reject('两次密码不一致')
                                    }
                                }
                            })
                        ]}
                    >
                        <Input.Password />
                    </Form.Item>
                    <Form.Item
                        label="昵称"
                        name="nickname"
                        rules={[ {required: false,message: '请输入昵称!'}]}
                    >
                        <Input.Password />
                    </Form.Item>
                    <Form.Item label={null}>
                        <Button type="primary" htmlType="submit">
                            注册
                        </Button>
                        <Link to="/login">已有账户,请登录</Link>
                    </Form.Item>
                </Form>
            </div>
        </div>
    )
}


export default Register

